const express = require('express');
const bodyParser = require('body-parser');

const mysql = require('mysql');
const app = express();
app.use(express.static('www'));
app.use(bodyParser.urlencoded({extended:false}));


const mysqli = myaql.createConnection( { 
    host:'localhost', //ip
    user:'root',
    password:'root',
    database:'lianxi'// 要连接的数据库名称
})
// 判断数据库是否连接成功
mysqli.connect(function(err){
    if(err){
        console.log('连接到数据库失败');
    }
})

app.post('/zhuce',(req,res)=>{
    console.log(req.body);
    var sql = 'select * from users';
    mysqli.query(sql,function(err,data){
        if(err){
            res.json({success:0,data:'注册失败(sql查询语句失败)'})
        }else{
            var isShow = true;
            for(var i = 0;i<data.length;i++){
                if(req.body.username == data[i].username){
                    isShow = false;

                }
            }
            if (isShow){
                var sql2= `insert into users(username,password)
                values('${req.body.username}',${req.body.password})`;
                mysqli.query(sql2,function(err2){
                    if(err){
                        res.json({success:0,data:'注册失败(新增的sql语句执行失败)'})
                    }else{
                        res.json({success:1,data:'注册成功'})
                    }
                })
            }else{ 
                res.json({success:0,data:'当前用户已存在'})
            }
        }
    })
})

app.post